﻿using Prism.Commands;
using Prism.Interactivity;
using Prism.Interactivity.InteractionRequest;
using SPW.GenericDesktop.Views;
using SPW.Infrastructure.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using SyntacticSugar;

namespace SPW.GenericDesktop.Actions
{
    /// <summary>
    /// 弹窗容器处理动作
    /// </summary>
    public class PopupContainerAction : PopupWindowAction
    {
        private Window _wrapperWindow;

        protected override Window GetWindow(INotification notification)
        {
            //参数转换
            Codes.PopupSettings popupSettings = notification as Codes.PopupSettings;

            //根据 NoBorder 创建初始窗口
            _wrapperWindow = NoBorder ? (Window)new NoBorderPopupContainer() : (Window)new GeneralPopupContainer();
            _wrapperWindow.Title = popupSettings.Title ?? $"新的弹窗 - {DateTime.Now.ToShortDateString()}";//设置标题
            _wrapperWindow.Owner = PlacementTarget;
            _wrapperWindow.ShowInTaskbar = popupSettings.ShowInTaskbar;
            if (popupSettings.WindowState == WindowState.Maximized) _wrapperWindow.WindowState = popupSettings.WindowState;
            else _wrapperWindow.SizeToContent = SizeToContent.WidthAndHeight;

            //设置内容
            if (_wrapperWindow.Content is ContentControl contentRegion
                && notification.Content is FrameworkElement element)
            {
                contentRegion.Content = element; //添加要素到内容

                //处理数据上下文
                if (element.DataContext is IInteractionRequestAware requestAware)
                {
                    requestAware.FinishInteraction = () =>
                    {
                        if (_wrapperWindow.IsValuable()) _wrapperWindow.Close();
                    };
                }
            }

            //尺寸大小变化时自动校准中心位置
            _wrapperWindow.SizeChanged += (o, e) =>
            {
                if (o is Window wind)
                {
                    double mainWidth = App.Current.MainWindow.ActualWidth;
                    double mainHeight = App.Current.MainWindow.ActualHeight;

                    wind.Left = (mainWidth - e.NewSize.Width) / 2;
                    wind.Top = (mainHeight - e.NewSize.Height) / 2;
                }
            };

            //是否能拖动
            if (popupSettings.IsDrag)
            {
                _wrapperWindow.MouseLeftButtonDown += (o, e) =>
                {
                    if (o is Window wind) wind.DragMove();
                };
            }

            //将传入的 notification  放入 window
            PrepareContentForWindow(notification, _wrapperWindow);

            return _wrapperWindow;
        }

        #region 外部扩展属性
        /// <summary>
        /// 窗口owner目标
        /// </summary>
        public Window PlacementTarget
        {
            get { return (Window)GetValue(PlacementTargetProperty); }
            set { SetValue(PlacementTargetProperty, value); }
        }

        public static readonly DependencyProperty PlacementTargetProperty =
            DependencyProperty.Register("PlacementTarget", typeof(Window), typeof(PopupContainerAction), new PropertyMetadata(null));

        /// <summary>
        /// 是否无边框
        /// </summary>
        public bool NoBorder
        {
            get { return (bool)GetValue(NoBorderProperty); }
            set { SetValue(NoBorderProperty, value); }
        }

        public static readonly DependencyProperty NoBorderProperty =
            DependencyProperty.Register("NoBorder", typeof(bool), typeof(PopupContainerAction), new PropertyMetadata(false));
        #endregion
    }
}
